<template>
  <div class="login-container">
    <div class="login_fruit"></div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left" label-width="62px">

      <div class="title-container">
        <div class="logo"></div>
        <h3 class="title">吉林四平溯源系统</h3>
      </div>

      <el-form-item label="用户名:">
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="请填写用户名"
          name="username"
          type="text"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item label="密 码:">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请填写密码"
          name="password"
          auto-complete="on"
          show-password
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import store from '@/store'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        loginName: '',
        password: ''
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          store.dispatch('user/Login', this.loginForm).then((res) => {
            this.$router.push({ path: this.redirect || '/basic' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
          // this.$router.push({ path: this.redirect || '/' })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-form-item__content {
    position: inherit;
    line-height: 0;
    background: #fff;
  }
  .el-input {
    display: inline-block;
    height: 40px;

    input {
      background: transparent;
      // border: 0px;
      -webkit-appearance: none;
      color: #333;
      height: 40px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/login_bg.jpg') 0 0 no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  .login_fruit {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 60%;
    height: 898px;
    background: url('../../assets/login_fruit.png') 0 100% no-repeat;
    background-size: contain;
  }

  .login-form {
    position: relative;
    width: 500px;
    height: 380px;
    max-width: 100%;
    padding: 30px 98px;
    margin: 0 auto;
    margin-top: 16%;
    margin-left: 54%;
    overflow: hidden;
    background: #fff;
    box-shadow: 0 0 5px rgba(0,0,0,.1);
    border-radius: 4px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  // .svg-container {
  //   padding: 6px 5px 6px 15px;
  //   color: $dark_gray;
  //   vertical-align: middle;
  //   width: 30px;
  //   display: inline-block;
  // }

  .title-container {
    position: relative;
    .logo {
      margin: 0 auto;
      width: 74px;
      height: 70px;
      background: url('../../assets/login_logo.png') 0 0 no-repeat;
      background-size: 74px 70px;
    }

    .title {
      font-size: 26px;
      color: #1eb955;
      margin: 10px auto 20px auto;
      text-align: center;
      font-weight: bold;
    }
  }
}
</style>
